package com.example.demo.controller;

import com.example.demo.custom_annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.pojo.Admin;
import com.example.demo.service.AdminService;

import javax.servlet.http.HttpServletResponse;

@CrossOrigin
@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	
	/*
	 * 后台登录接口
	 * */
	@Log("管理员登录")
	@RequestMapping(value="/login")
	@ResponseBody
	public Integer loginCheck(@RequestBody Admin admin, HttpServletResponse response) {
		//server端返回的结果
		String result;
		//在返回的请求头中加入该请求的角色
		String role;
		int row;
		String checkBack;
		//打印从前台得到的admin账号密码信息
		System.out.println(admin);
		//创建一个admin基础类对象
		Admin adminInfo=new Admin();
		//admin对象提取得到账号密码
		adminInfo.setAdminAccount(admin.getAdminAccount());
		adminInfo.setAdminPassword(admin.getAdminPassword());
		checkBack=admin.getAdminAccount();
		//server端返回结果给result
		result=adminService.loginCheck(adminInfo);
		//简单验证
		if(result.equals("ok")) {
			System.out.println("账号存在！密码正确！登录成功！");
			row=1;
			role = "admin";
			response.setHeader("role",role);
		}else {
			System.out.println("账号或密码错误！");
			row=0;
		}
		return row;
	}
}
